{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Testing if a Distribution is Normal"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import scipy.stats as stats\n",
    "import matplotlib.pyplot as plt\n",
    "import quiz_tests\n",
    "\n",
    "# Set plotting options\n",
    "%matplotlib inline\n",
    "plt.rc('figure', figsize=(16, 9))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create normal and non-normal distributions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Sample A: Normal distribution\n",
    "sample_a = stats.norm.rvs(loc=0.0, scale=1.0, size=(1000,))\n",
    "\n",
    "# Sample B: Non-normal distribution\n",
    "sample_b = stats.lognorm.rvs(s=0.5, loc=0.0, scale=1.0, size=(1000,))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Quiz Solution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Quiz\n",
    "\n",
    "To use the Kolmogorov-Smirnov test, complete the function `is_normal_ks`.\n",
    "\n",
    "To set the variable normal_args, create a tuple with two values.  An example of a tuple is `(\"apple\",\"banana\")`\n",
    "The first is the mean of the sample. The second is the standard deviation of the sample.\n",
    "\n",
    "**hint:** Hint: Numpy has functions np.mean() and np.std()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def is_normal_ks(sample, test=stats.kstest, p_level=0.05, **kwargs):    \n",
    "    \"\"\"\n",
    "    sample: a sample distribution\n",
    "    test: a function that tests for normality\n",
    "    p_level: if the test returns a p-value > than p_level, assume normality\n",
    "    \n",
    "    return: True if distribution is normal, False otherwise\n",
    "    \"\"\"\n",
    "    normal_args = (np.mean(sample),np.std(sample))\n",
    "    \n",
    "    t_stat, p_value = test(sample, 'norm', normal_args, **kwargs)\n",
    "    print(\"Test statistic: {}, p-value: {}\".format(t_stat, p_value))\n",
    "    print(\"Is the distribution Likely Normal? {}\".format(p_value > p_level))\n",
    "    return p_value > p_level\n",
    "\n",
    "quiz_tests.test_is_normal_ks(is_normal_ks)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Using Kolmogorov-Smirnov test\n",
    "print(\"Sample A:-\"); is_normal_ks(sample_a);\n",
    "print(\"Sample B:-\"); is_normal_ks(sample_b);"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
